package com.rainng.coursesystem.dao.mapper;


import com.rainng.coursesystem.repository.PostEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {

    // 查询指定课程的所有帖子
    @Select("SELECT * FROM rc_post WHERE courseid = #{courseId} AND deleted_at IS NULL")
    List<PostEntity> getPostsByCourseId(@Param("courseId") int courseId);

    // 根据帖子ID查询帖子
    @Select("SELECT * FROM rc_post WHERE post_id = #{postId}")
    PostEntity getPostById(@Param("postId") int postId);

    // 查询所有帖子（可以根据条件筛选）
    @Select("SELECT * FROM rc_post WHERE deleted_at IS NULL")
    List<PostEntity> getAllPosts();

    // 创建新帖子

    @Insert("INSERT INTO rc_post(courseid, studentid, content, image_url, title, created_at) " +
            "VALUES(#{courseId}, #{studentId}, #{content}, #{imageUrl}, #{title}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "postId")  // 自动生成主键
    void createPost(PostEntity post);

    // 删除帖子（软删除，设置deleted_at）
    @Delete("DELETE FROM rc_post WHERE post_id = #{postId}")
    void deletePost(@Param("postId") int id);


    // 更新帖子
    @Update("UPDATE rc_post SET title = #{title}, content = #{content}, image_url = #{imageUrl} WHERE post_id = #{postId}")
    void updatePost(PostEntity post);
}

